package edu.ucsd.cs110.mycity.controller;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

import edu.ucsd.cs110.mycity.model.data.CoordinateListener;
import edu.ucsd.cs110.mycity.model.data.TRKPTCoordinate;

public class CoordinateController {
	
	private static Map<String,TRKPTCoordinate> coordinates = new TreeMap<String,TRKPTCoordinate>();
	private static List<CoordinateListener> listeners = new ArrayList<CoordinateListener>();
	
	public static void setCoordinates(Map<String,TRKPTCoordinate> coordinates) {
		CoordinateController.coordinates = coordinates;
	}

	public static Map<String,TRKPTCoordinate> getCoordinates() {
		return coordinates;
	}
	
	public static void updateCoordinates(String user,TRKPTCoordinate coordinate) {
		coordinates.put(user, coordinate);
		fire();
	}
	
	public static void fire() {
		Iterator<CoordinateListener> iter = listeners.iterator();
		while ( iter.hasNext() ) {
			CoordinateListener c = iter.next();
			c.run();
		}
	}
	
	public static void addCoordinateListener(CoordinateListener listener) {
		listeners.add(listener);
	}
	
	public static void removeCoordinateListener(CoordinateListener listener) {
		listeners.remove(listener);
	}
	
}